using System;
using System.Data;
using System.Drawing;
using System.Configuration;
using System.Configuration.Assemblies;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.Services.Protocols;
using System.Xml;
using MSU_UserControls.MSUWS;

namespace MSU_UserControls
{
    [System.ComponentModel.Description("MSU Change Password")]
    public partial class ChangePassword : MSU_UserControl, SmartPart.IConnectionConsumerControl
    {
        
        #region IConnectionConsumerControl Members

        public string ConsumerMenuLabel
        {
            get { return "Receives text data from"; }
        }

        public void SetConsumerData(object data)
        {
            if (data.GetType() == typeof(bool))
            {
                //if ((MSU_User)data == null)
                if (!(bool)data)
                {
                    pnPassword.Visible = false;
                    pnMessage.Visible = true;
                }
                else
                {
                    pnPassword.Visible = true;
                    pnMessage.Visible = false;
                }
                //lblMessage.Text = data.ToString(); 
                
                ////TextBox1.Text = data.ToString();
            }
        }

        #endregion        
        
        #region Events(Page_Load, btnChangepassword_Click)

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnChangepassword_Click(object sender, EventArgs ea)
        {
            if (txtNewPassword.Text == txtNewPasswordConfirm.Text)
            {
                try
                {
                    WSMSU.ChangeUserPassword(base._msu_Adm, base._msu_User, txtOldPassword.Text, txtNewPassword.Text);
                    lblresult.Text = "Password Changed.";
                    lblresult.ForeColor = Color.Blue;
                }
                catch (SoapException soapEx)
                {
                    //Load the Detail element of the SoaopException object
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(soapEx.Detail.OuterXml);

                    XmlNamespaceManager nsManager = new
                    XmlNamespaceManager(doc.NameTable);

                    // Add the namespace to the NamespaceManager
                    nsManager.AddNamespace("errorNS", "http://tempuri.org/MSUWS");

                    XmlNode categoryNode = doc.DocumentElement.SelectSingleNode("errorNS:Error", nsManager);
                    string errorTypeNumber = categoryNode.SelectSingleNode("errorNS:ErrorTypeNumber", nsManager).InnerText;
                    string message = categoryNode.SelectSingleNode("errorNS:Message", nsManager).InnerText;
                    string stackTrace = categoryNode.SelectSingleNode("errorNS:StackTrace", nsManager).InnerText;
                    string innerError = categoryNode.SelectSingleNode("errorNS:InnerError", nsManager).InnerText;
                    string onScreen = categoryNode.SelectSingleNode("errorNS:OnScreen", nsManager).InnerText;

                    if (string.Compare(onScreen, true.ToString()) == 0)
                    {
                        lblresult.Text += message + "\r\n";
                        lblresult.ForeColor = Color.DarkRed;
                    }
                    else
                    {
                        lblresult.Text += "Unexpected error. Details:\r\n";
                        lblresult.Text += "Soap fault code: " + soapEx.Code.ToString() + "\r\n";
                        lblresult.Text += "ErrorType Number is: " + errorTypeNumber + "\r\n";
                        lblresult.Text += "Error Message is: " + message + "\r\n";
                        lblresult.Text += "Error StackTrace is: " + stackTrace + "\r\n";
                        lblresult.Text += "Error InnerError is: " + (string.IsNullOrEmpty(innerError) ? "null" : innerError) + "\r\n";
                        lblresult.ForeColor = Color.Red;
                    }
                }
                catch (Exception ex)
                {
                    lblresult.Text += "Unexpected error. Details:\r\n";
                    lblresult.Text += "Error Message is: " + ex.Message + "\r\n";
                    lblresult.Text += "Error StackTrace is: " + ex.StackTrace + "\r\n";
                    lblresult.Text += "Error InnerException is: " + ((ex.InnerException == null) ? "None" : ex.InnerException.Message) + "\r\n";
                    lblresult.ForeColor = Color.Red;
                }
            }
        } 

        #endregion

    }
}

